home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000020.txt < prev    next >
Text File  |  2013-04-03  |  4KB  |  120 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the chrome.app.runtime API.
  6.  
  7. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  8. var fileSystemHelpers = requireNative('file_system_natives');
  9. var GetIsolatedFileSystem = fileSystemHelpers.GetIsolatedFileSystem;
  10. var appNatives = requireNative('app_runtime');
  11. var DeserializeString = appNatives.DeserializeString;
  12. var SerializeToString = appNatives.SerializeToString;
  13. var CreateBlob = appNatives.CreateBlob;
  14.  
  15. chromeHidden.Event.registerArgumentMassager('app.runtime.onLaunched',
  16.     function(args, dispatch) {
  17.   var launchData = args[0];
  18.   var intentData = args[1];
  19.   var intentId = args[2];
  20.  
  21.   if (launchData && typeof launchData.id !== 'undefined') {
  22.     // new-style dispatch.
  23.     var items = []
  24.     var numItems = launchData.items.length;
  25.     var itemLoaded = function(err, item) {
  26.       if (err) {
  27.         console.error('Error getting fileEntry, code: ' + err.code);
  28.       } else {
  29.         items.push(item);
  30.       }
  31.       if (--numItems === 0) {
  32.         if (items.length === 0) {
  33.           dispatch([]);
  34.         } else {
  35.           var data = { id: launchData.id, items: items };
  36.           // TODO(benwells): remove once we no longer support intents.
  37.           data.intent = {
  38.             action: "http://webintents.org/view",
  39.             type: "chrome-extension://fileentry",
  40.             data: items[0].entry,
  41.             postResult: function() {},
  42.             postFailure: function() {}
  43.           };
  44.           dispatch([data]);
  45.         }
  46.       }
  47.     };
  48.     launchData.items.forEach(function(item) {
  49.       var fs = GetIsolatedFileSystem(item.fileSystemId);
  50.       fs.root.getFile(item.baseName, {}, function(fileEntry) {
  51.         itemLoaded(null, { entry: fileEntry, type: item.mimeType });
  52.       }, function(fileError) {
  53.         itemLoaded(fileError);
  54.       });
  55.     });
  56.   } else {
  57.     if (launchData) {
  58.       if (intentId) {
  59.         var fn = function(success, data) {
  60.           chrome.app.runtime.postIntentResponse({
  61.             'intentId': intentId,
  62.             'success': success,
  63.             'data': SerializeToString(data)
  64.           });
  65.         };
  66.         launchData.intent.postResult = fn.bind(undefined, true);
  67.         launchData.intent.postFailure = fn.bind(undefined, false);
  68.       } else {
  69.         launchData.intent.postResult = function() {};
  70.         launchData.intent.postFailure = function() {};
  71.       }
  72.     }
  73.  
  74.     if (launchData && intentData) {
  75.       switch(intentData.format) {
  76.         case('fileEntry'):
  77.           var fs = GetIsolatedFileSystem(intentData.fileSystemId);
  78.           try {
  79.             fs.root.getFile(intentData.baseName, {}, function(fileEntry) {
  80.               launchData.intent.data = fileEntry;
  81.               dispatch([launchData]);
  82.             }, function(fileError) {
  83.               console.error('Error getting fileEntry, code: ' + fileError.code);
  84.               dispatch([]);
  85.             });
  86.           } catch (e) {
  87.             console.error('Error in event handler for onLaunched: ' + e.stack);
  88.             dispatch([]);
  89.           }
  90.           break;
  91.         case('filesystem'):
  92.           launchData.intent.data = GetIsolatedFileSystem(
  93.               intentData.fileSystemId, intentData.baseName);
  94.           launchData.intent.postResult = function() {};
  95.           launchData.intent.postFailure = function() {};
  96.           dispatch([launchData]);
  97.           break;
  98.         case('serialized'):
  99.           var deserializedData = DeserializeString(intentData.data);
  100.           launchData.intent.data = deserializedData;
  101.           dispatch([launchData]);
  102.           break;
  103.         case('blob'):
  104.           var blobData = CreateBlob(intentData.blobFilePath,
  105.                                     intentData.blobLength);
  106.           launchData.intent.data = blobData;
  107.           dispatch([launchData]);
  108.           break;
  109.         default:
  110.           console.error('Unexpected launch data format');
  111.           dispatch([]);
  112.       }
  113.     } else if (launchData) {
  114.       dispatch([launchData]);
  115.     } else {
  116.       dispatch([]);
  117.     }
  118.   }
  119. });
  120.